//
//  LXMyGreenBeansViewController.m
//  LvXi_newVersion
//
//  Created by 诸葛孔明 on 16/9/6.
//  Copyright © 2016年 SJT. All rights reserved.
//............................................................................................//
//...........................................个人中心-我的绿豆...........................................//
//............................................................................................//

#import "LXMyGreenBeansViewController.h"

#import "LXGreenBeansCountViewCell.h"

#import "LXMineViewController.h"

#import "GreenBeanDetaiilCel.h"

static NSString *const LXGreenBeansCountViewCellID = @"LXGreenBeansCountViewCell";

@interface LXMyGreenBeansViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak) UITableView *tableView;

@property (nonatomic,weak) NSString *greenBeansNums;

@property(nonatomic,strong)NSArray * detailArray;

@end

@implementation LXMyGreenBeansViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.greenBeansNums = [LXUser shareUser].bean_nums;
    self.detailArray = [NSArray array];
    [self setUpTableView];
    
    
    [[LXDataManager manager] getGreenBeanDetailWithComplete:^(BOOL success, id res) {
        if(success){
            self.detailArray = res;
            [self.tableView reloadData];
        }
    }];
}
-(void)setUpNavi{
    [super setUpNavi];
    self.navigationItem.title = @"我的绿豆";
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setUpNavi];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)setUpTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [self.tableView registerClass:[LXGreenBeansCountViewCell class] forCellReuseIdentifier:LXGreenBeansCountViewCellID];
    [self.tableView registerClass:[GreenBeanDetaiilCel class] forCellReuseIdentifier:@"GreenBeanDetaiilCel"];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) return 1;
    return self.detailArray.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        LXGreenBeansCountViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LXGreenBeansCountViewCellID];
        
        cell.countLabel.text = self.greenBeansNums;
        cell.enableUnaderLine = NO;
        cell.roundStyle = RoundStyleAll;
        return cell;
    }
    else{
        if(indexPath.row==0){
            LXNoHighLightViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:@"LXNoHighLightViewCell"];
            if(!cell){
                cell = [[LXNoHighLightViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LXNoHighLightViewCell"];
            }
            cell.enableUnaderLine = YES;
            cell.roundStyle = RoundStyleTop;
            cell.textLabel.text = @"绿豆明细";
            return cell;
        }else{
            GreenBeanDetaiilCel * cell = [tableView dequeueReusableCellWithIdentifier:@"GreenBeanDetaiilCel"];
            if(indexPath.row == 1){
                cell.enableUnaderLine = YES;
                cell.roundStyle = RoundStyleNone;
            }else if ((indexPath.row-1) == self.detailArray.count-1){
                cell.enableUnaderLine = NO;
                cell.roundStyle = RoundStyleBottom;
                
            }else{
                cell.enableUnaderLine = YES;
                cell.roundStyle = RoundStyleNone;
            }
            GreenBeanDetailModel * model = self.detailArray[indexPath.row-1];
            cell.typeLabel.text = model.mtype;
            cell.timeLabel.text = model.create_time;
            cell.greenBeanLabel.text = model.beans;
            [cell setNeedsLayout];
            return cell;

        }
    }
    return [[UITableViewCell alloc] init];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) return MineRatio(125);
    if(indexPath.section==1&&indexPath.row==0) return MineRatio(40);
    return MineRatio(50);
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

@end
